package com.flute.framework;

import com.flute.framework.configuration.MonitorXMLConverter;
import com.flute.framework.exception.ConfigurationParseException;
import com.flute.framework.monitor.EventLogger;
import com.flute.framework.monitor.MonitorService;
import com.flute.tools.util.ExceptionUtil;

public class FluteMonitor {
	private static final String INDEXER = "indexer";
	private static final String SEARCHER = "searcher";
	private static final String SEARCHER_PROVIDER = "provider";
	private static final String RECIEVER = "reciever";
	private static final String IMPORTER = "importer";

	private static MonitorService service;

	static {
		try {
			service = new MonitorService(MonitorXMLConverter.class
					.getClassLoader().getResourceAsStream("config/monitor.xml"));
		} catch (ConfigurationParseException e) {
			EventLogger.error("加载monitor配置文件失败."
					+ ExceptionUtil.getExceptionContent(e),
					EventLogger.MONITOR_KEY);
		}

		try {
			service.start();
		} catch (Exception e) {
			EventLogger.error("注册事件到服务器失败."
					+ ExceptionUtil.getExceptionContent(e),
					EventLogger.MONITOR_KEY);
		}
	}

	public static String getIndexerKey() {
		if (!service.getDefinition().containsEvent(INDEXER)) {
			EventLogger.error("monitor not defined, '" + INDEXER + "'",
					EventLogger.MONITOR_KEY);
		}
		return INDEXER;
	}

	public static String getRecieverKey() {
		if (!service.getDefinition().containsEvent(RECIEVER)) {
			EventLogger.error("monitor not defined, '" + RECIEVER + "'",
					EventLogger.MONITOR_KEY);
		}
		return RECIEVER;
	}

	public static String getImporterKey() {
		if (!service.getDefinition().containsEvent(IMPORTER)) {
			EventLogger.error("monitor not defined, '" + IMPORTER + "'",
					EventLogger.MONITOR_KEY);
		}
		return IMPORTER;
	}

	public static String getSearcherKey() {
		if (!service.getDefinition().containsEvent(SEARCHER)) {
			EventLogger.error("monitor not defined, '" + SEARCHER + "'",
					EventLogger.MONITOR_KEY);
		}
		return SEARCHER;
	}

	public static String getSearcherProviderKey() {
		if (!service.getDefinition().containsEvent(SEARCHER_PROVIDER)) {
			EventLogger.error("monitor not defined, '" + SEARCHER_PROVIDER + "'",
					EventLogger.MONITOR_KEY);
		}
		return SEARCHER_PROVIDER;
	}
}
